package com.seeyon.apps.kk.cap4Widget;

import java.util.List;

import org.apache.commons.logging.Log;

import com.seeyon.cap4.form.bean.FormAuthViewFieldBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.cap4.form.bean.ParamDefinition;
import com.seeyon.cap4.form.bean.fieldCtrl.FormFieldCustomCtrl;
import com.seeyon.cap4.form.util.Enums.ParamType;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.filemanager.manager.AttachmentManager;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.util.Strings;
import com.seeyon.ctp.util.UUIDLong;

/**
 * <pre>
 * 自定义控件:BIM选择构件
 * </pre>
 */
public class OCRCustomWidget extends FormFieldCustomCtrl {
	
	private static final Log LOGGER = CtpLogFactory.getLog(OCRCustomWidget.class);
	
	@Override
    public String getKey() {
        return "7827871416804946708";
    }
    
	@Override
    public String getText() {
        return "合合OCR表单控件";
    }
	/*
	@Override
    public String getFieldLength() {
        return "10";
    }

	@Override
    public void init() {
		FormOcrCtrl a;
        setPluginId("OCRCustom");
		this.setIcon("cap-icon-querystatistics");
		ParamDefinition statsParam = new ParamDefinition();
		statsParam.setDialogUrl("seep.do?method=ocrPlug");
		statsParam.setDisplay("字段对应关系");
		statsParam.setName("orcConfig");
		statsParam.setParamType(ParamType.button);
		statsParam.setDialogHeight("");
		statsParam.setDialogWidth("500");
		this.addDefinition(statsParam);
    }

    @Override
	public String getIcon() {
		return "";
	}

    @Override
	public String getPCInjectionInfo() {
    	Map<String, String> injectInfo = new HashMap();
		injectInfo.put("path", "apps_res/cap/OCRCustom/");
		injectInfo.put("jsUri", "js/location.js");
		injectInfo.put("initMethod", "init");
		injectInfo.put("nameSpace", "field_" + this.getKey());
		return JSONUtil.toJSONString(injectInfo);
    }

    @Override
    public String getMBInjectionInfo() {
        return null;//"{path:'http://mapResource.v5.cmp/v1.0.0/',weixinpath:'invoice',jsUri:'js/location.js',initMethod:'init',nameSpace:'feild_"+this.getKey()+"'}";
    }

    @Override
    public boolean canBathUpdate() {
        return false;
    }

    @Override
    public List<String[]> getListShowDefaultVal(Integer externalType) {
        return null;
    }

    @Override
    public String[] getDefaultVal(String defaultValue) {
        return new String[0];
    }

    @Override
    public boolean canInjectionWord() {
        return false;
    }*/

    @Override
    public boolean canBathUpdate() {
        return false;
    }

    @Override
    public boolean isAttachment() {
        return true;
    }

    /**
     * 新建的时候生成此自定义控件的值，此值会存放在对应动态表的对应字段上。
     *
     * @param oldVal
     * @return
     */
    @Override
    public Object genVal(Object oldVal) {
        if (Strings.isEmpty(String.valueOf(oldVal))) {
            return UUIDLong.longUUID();
        } else {
            return oldVal;
        }
    }

    @Override
    public List<String[]> getListShowDefaultVal(Integer integer) {
        return null;
    }

    @Override
    public String[] getDefaultVal(String s) {
        return new String[0];
    }

    @Override
    public boolean authNotNullAndValIsNull(FormDataMasterBean formDataMasterBean, FormFieldBean field, FormAuthViewFieldBean authViewFieldBean, Object val) {
        String strVal = String.valueOf(val);
        boolean result = false;
        if(Strings.isEmpty(strVal)&&authViewFieldBean.isNotNullable()){
            result = true;
        }else{
            AttachmentManager attachmentManager = (AttachmentManager) AppContext.getBean("attachmentManager");
            result = authViewFieldBean.isNotNullable()&&attachmentManager.getBySubReference(Long.parseLong(strVal)).size()<=0;
        }
        return result;
    }

    @Override
    public void init() {
        this.setPluginId("formOcrCtrl");
        this.setIcon("cap-icon-credentials-recognition");
        ParamDefinition eivoiceMap = new ParamDefinition();
        eivoiceMap.setDialogUrl("apps_res/cap/ocrCustom/html/ocrCtrlSetting.html");
        eivoiceMap.setDisplay("数据回写设置");
        eivoiceMap.setName("mapping");
        eivoiceMap.setParamType(ParamType.button);
        addDefinition(eivoiceMap);
        LOGGER.info("自定义控件" + this.getText() + "init执行结束，params.size:" + super.params.size());
    }

    @Override
    public String getPCInjectionInfo() {
        return "{path:'apps_res/cap/ocrCustom/',jsUri:'js/ocrCtrPcRunning.js',initMethod:'init',nameSpace:'field_" + this.getKey() + "'}";
    }

    @Override
    public String getMBInjectionInfo() {
        //return "{path:'http://ocrbtn.v5.cmp/v/',weixinpath:'ocrbtn/',jsUri:'js/ocrCtrM3Running.js',initMethod:'init',nameSpace:'field_" + this.getKey() + "'}";
    	return null;
    }

    @Override
    public String getFieldLength() {
        return "20";
    }

    @Override
    public boolean canInjectionWord() {
        return false;
    }
}
